using System;
using System.Windows.Forms;

using FreeFlow.Client;

namespace FreeFlow.WinFormsControls
{
	/// <summary>
	/// A check box control.
	/// </summary>
	public class CheckBoxControl : CheckBox, IFillable, IRefillable
	{
    /// <summary>
    /// Creates a new <see cref="CheckBoxControl"/> instance.
    /// </summary>
		public CheckBoxControl()
		{
    }
    #region IFillable Members

    /// <summary>
    /// Renders the checkbox with static layout information using the specified field.
    /// </summary>
    /// <param name="field">Field.</param>
    public void Render(FreeFlow.Client.Field field)
    {
      AutoSize = true;
      Text = (field as CheckField).Caption;
    }

    /// <summary>
    /// Fills the checkbox with values from the specified field.
    /// </summary>
    /// <param name="field">Field.</param>
    public void Fill(FreeFlow.Client.Field field)
    {
      if (field != null)
      {
        Checked = (field.Value == "1");
      }
    }

    /// <summary>
    /// Fills the field from the control.
    /// </summary>
    /// <param name="field">Field.</param>
    public void FillField(Field field)
    {
      if (field != null)
      {
        if (Checked)
          field.Value = "1";
        else
          field.Value = "0";
      }
    }

    #endregion
    #region IRefillable Members

    /// <summary>
    /// Event triggered when a refill should occur
    /// </summary>
    public event System.EventHandler Refill;

    /// <summary>
    /// Triggers a refill.
    /// </summary>
    /// <param name="e">E.</param>
    protected override void OnCheckedChanged (EventArgs e)
    {
      base.OnCheckedChanged(e);
      if (Refill != null)
        Refill(this, e);
    }

    #endregion
  }
}
